package cn.kgc.ybis.controller;

import cn.kgc.ybis.pojo.Class;
import cn.kgc.ybis.pojo.*;
import cn.kgc.ybis.service.ReportService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
public class ReportController {
    @Autowired
    ReportService reportService;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
    private SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @RequestMapping("/selectClass")
    public String selectClass( Model model,HttpSession session){
        System.out.println("jinlaole");
        List<Class> classes = reportService.selectClass(1);
        session.setAttribute("classes",classes);
        return "teacherReport";
    }
    @RequestMapping("/isTM")
    public String isTM(Model model,Integer cId,HttpSession session){
        Boolean tm = reportService.isTeacherManager(1, cId);
        session.setAttribute("tm",tm);
        session.setAttribute("cId",cId);
        return "forward:page";
    }
    @RequestMapping("/page")
    public String page(String likename,Model model,Integer pageNum,Integer pageSize,HttpSession session){
        Integer cId = (Integer) session.getAttribute("cId");
        String likename1 ="";
        if (likename!=null){
            likename1 = likename;
            model.addAttribute("likename",likename1);
        }
        Integer pageNum1 =1;
        if (pageNum!=null){
            pageNum1 = pageNum;
        }
        Integer pageSize1 = 3;
        if (pageSize!=null){
            pageSize1 = pageSize;
        }
        PageInfo<Download> pageInfo = reportService.reportPage(cId, likename1, pageNum1, pageSize1);
        if (pageInfo.getList()==null || pageInfo.getList().size()<=0){
            model.addAttribute("msg","对不起，没有符合条件的查询");
        }
        model.addAttribute("cId",cId);
        model.addAttribute("pageInfo",pageInfo);
        return "reportAll";
    }
    @RequestMapping("/add")
    public String add(Model model, String title, Integer cId, MultipartFile idpic, HttpSession session){
        Date date = new Date();
        //获取文件上传路径
        String realPath = session.getServletContext().getRealPath("/excel/");
        //获取上传文件原名
        String oldName = idpic.getOriginalFilename();
        //获取文件拓展名
        String extension = FilenameUtils.getExtension(oldName);
        //验证文件格式
        List<String> prefixs = new ArrayList<>();
        prefixs.add("xlsx");
        if (prefixs.contains(extension)==false){
            model.addAttribute("uploadFileError"," * 上传图片格式不正确");
            return "forward:page";
        }
        //生成文件新名称
        String newName = "Time_"+simpleDateFormat.format(date)+"_"+new Random().nextInt(100000)+"_cId_"+cId+"_"+title+"."+ extension;
        //封装File对象

        File file = new File(realPath,newName);
        //实现上传
        try {
            idpic.transferTo(file);
            model.addAttribute("msg","文件上传成功");
            model.addAttribute("newName",newName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "forward:write?newName="+newName+"&date="+simpleDateFormat1.format(date);
    }
    //下载模板
    //并更新
    @RequestMapping("copy")
    @ResponseBody
    public void copy(HttpServletRequest request,HttpServletResponse response,Integer cId){
        String fileName = "模板.xlsx";
        //获取文件路径
        String realPath = request.getServletContext().getRealPath("excel/");
        //修改模板
        reportService.excelTemplate(cId,realPath+fileName);
        //封装File对象
        File file = new File(realPath,fileName);
        //设置响应类型  ==》 告诉浏览器当前是下载操作，我要下载东西
        response.setContentType("application/x-msdownload");//文件HTML类型可改
        //设置下载时文件的显示类型(即文件名称-后缀)   ex：txt为文本类型
        response.setHeader("Content-Disposition", "attachment;filename=" +fileName);
        //下载文件：将一个路径下的文件数据转到一个输出流中，也就是把服务器文件通过流写(复制)到浏览器端
        try {
            Files.copy(file.toPath(),response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //excel成绩写入数据库
    @RequestMapping("/write")
    public String write(HttpServletRequest request, HttpSession session, String title,String newName, String date) throws ParseException {
        //获取文件上传路径
        String realPath = session.getServletContext().getRealPath("excel/");
        String pathAll = realPath + newName;
        reportService.write(pathAll,title,newName,date);


        return "forward:page";
    }
    //批量删除
    @RequestMapping("deleteAll")
    public String deleteAll(String idstr,Model model){
        String[] split = idstr.split(",");
        List<Integer> list = new ArrayList<>();
        for (String s : split) {
            System.out.println( s );
            list.add(Integer.parseInt(s));
        }
        Boolean deleteAll = reportService.deleteAll(list);
        if (!deleteAll){
            model.addAttribute("msg","删除失败");
        }
        return "forward:page";
    }
    //ajax模糊查询学生姓名
    @RequestMapping("ajaxUserName")
    @ResponseBody
    public List<User> ajaxUserName(String name,HttpSession session){
        Integer cId = (Integer) session.getAttribute("cId");
        System.out.println(cId);
        UserExample example = new UserExample();
        example.createCriteria().andCIdEqualTo(cId).andUNameLike("%"+name+"%");
        List<User> users = reportService.ajaxUserName(example);
        System.out.println(users.toString());
        return users;
    }
    //ajax模糊查询学生成绩
//    @RequestMapping("ajaxScore")
//    @ResponseBody
//    public List<Report> ajaxScore(String name,String typeId,String biaoti,String userPath){
//        Integer type = Integer.parseInt(typeId);
//        UserExample userExample = new UserExample();
//        userExample.createCriteria().andUNameEqualTo(name).andUPathEqualTo(userPath);
//        List<User> users = reportService.ajaxUserName(userExample);
//        ReportExample example = new ReportExample();
//        example.createCriteria().andUIdEqualTo(users.get(0).getuId()).andTTypeEqualTo(type).andTTitleEqualTo(biaoti);
//        List<Report> reports = reportService.ajaxScore(example);
//        return  reports;
//    }
    //下载成绩单
    @RequestMapping("down")
    @ResponseBody
    public void down(HttpServletRequest request,HttpServletResponse response,String downPath){
        //获取文件路径
        String realPath = request.getServletContext().getRealPath("excel/");
        //封装File对象
        File file = new File(realPath,downPath);
        //设置响应类型  ==》 告诉浏览器当前是下载操作，我要下载东西
        response.setContentType("application/x-msdownload");//文件HTML类型可改
        //设置下载时文件的显示类型(即文件名称-后缀)   ex：txt为文本类型
        response.setHeader("Content-Disposition", "attachment;filename=" +downPath);
        //下载文件：将一个路径下的文件数据转到一个输出流中，也就是把服务器文件通过流写(复制)到浏览器端
        try {
            Files.copy(file.toPath(),response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("deleteOne")
    public String delete(String downId){
        reportService.deleteOne(downId);
        return "forward:page";
    }
    @RequestMapping("excelPage")
    public String excelPage(HttpSession session,String downPath,Model model,String likename,String updatename,String updatetype,String updatescore){
        String realPath = session.getServletContext().getRealPath("excel/");
        Integer cId = (Integer) session.getAttribute("cId");
        session.setAttribute("cId",cId);
        String pathAll = realPath + downPath;
        List<GPS> list = reportService.excelPage(pathAll,updatename,updatetype,updatescore);
        if (likename!=null && likename.length()>0){
            List<GPS> list1 = new ArrayList<>();
            for (GPS gps : list) {
                if (gps.getuName().contains(likename)){
                    list1.add(gps);
                }
            }
            model.addAttribute("list",list1);
        }else {
            model.addAttribute("list",list);
        }
        model.addAttribute("downPath",downPath);
        return "scoreAll";
    }
    @RequestMapping("zhongzhuan")
    public String zhongzhuan(String downPath,Model model){
        model.addAttribute("downPath",downPath);
        return "reportUpdate";
    }
    @RequestMapping("update")
    public String update(String name,String type,String downPath,String score) throws ParseException {
        List<User> users = reportService.selectUId(name);
        String[] split = downPath.split("_");
        String creatTime = split[1];
        String[] split1 = creatTime.split(" ");
        String replace = split1[1].replace('-', ':');
        replace = split1[0]+" "+replace;
        reportService.updateReport(users.get(0).getuId(),Integer.parseInt(type),replace,Integer.parseInt(score));
        return "forward:excelPage?updatename="+name+"&updatetype="+type+"&updatescore="+score;
    }
    @RequestMapping("selectOneReport")
    public String selectOneReport(String type,String downPath,Model model) throws ParseException {
        if (type.equals("0")){
            return "forward:excelPage";
        }
        String[] split = downPath.split("_");
        String creatTime = split[1];
        String[] split1 = creatTime.split(" ");
        String replace = split1[1].replace('-', ':');
        replace = split1[0]+" "+replace;
        List<Report> reports = reportService.selectOneReport(Integer.parseInt(type), replace);
        model.addAttribute("reports",reports);
        for (Report report : reports) {
            System.out.println( "======================" );
            System.out.println( report );
        }
        model.addAttribute("type",type);
        return "selectOneReport";
    }



    //
    //
    //
    /**
     * 学生端
     * 郭鹏生
     *
     */
    @RequestMapping("userSelectReport")
    public String userSelectReport(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        Integer cId = user.getcId();
        Integer uId = 1;
        String creatTime ="";
        List<String> creatTimeList = new ArrayList<>();
        List<Download> downloads = reportService.selectDownload();
        List<Download> downloadList = new ArrayList<>();
        for (Download download : downloads) {
            String downPath = download.getDownPath();
            if (downPath.contains("cId_"+cId)){
                String[] split = downPath.split("_");
                String[] split1 = split[1].split(" ");
                String replace = split1[1].replace('-', ':');
                creatTime = split1[0]+replace;
                creatTimeList.add(creatTime);
            }
        }
        List<List<Report>> reportsList = new ArrayList<>();
        for (int i = 0; i < creatTimeList.size(); i++) {
            List<Report> reports= reportService.userSelectReport(uId, creatTimeList.get(i));
            reportsList.add(reports);
        }
        model.addAttribute("reportsList",reportsList);
        return "userReport";
    }
}
